<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :before-close="handleClose"
      :visible.sync="visible"
      width="300px"
    >
      <el-tree :data="tree.list" :props="tree.props" icon-class="el-icon-folder el-icon-folder-opened" @node-click="handleNodeClick" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSelected">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import TreeUtils from '@/utils/TreeUtils'
import OrgApi from '@/api/system/OrgApi'

export default {
  directives: {
    waves
  },
  props: {
    visible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      title: '机构选择器',
      tree: {
        list: [],
        currentRow: undefined,
        props: {
          children: 'children',
          label: 'orgName'
        }
      }
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData() {
      OrgApi.list(this.params, this.page).then(response => {
        this.tree.list = TreeUtils.list2Tree(response.data)
      })
    },
    handleNodeClick(row) {
      this.tree.currentRow = row
    },
    handleSelected() {
      if (this.tree.currentRow) {
        this.$emit('selected', this.tree.currentRow)
      }
      this.handleClose()
    },
    handleClose() {
      this.tree.currentRow = undefined
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  // 高级查询
  .filter_container {
    padding-bottom: 0;
    .filter_row {
      .el-row {
        .el-col {
          .el-form-item{
            margin-bottom: 0;
          }
        }
      }
    }
  }
  // 操作按钮
  .operation_container {
    ul {
      margin-top: 0;
      li {
        float: right;
        list-style: none;
        padding-right: 2px;
        button {
          margin-bottom: 4px;
        }
      }
    }
  }
</style>
